//
//  YLARatingView.swift
//  Your Lover AI
//
//  Created by 阿忠 on 2024/7/2.
//

import Foundation
import UIKit

/**
 修改名字的弹窗
 */
class YLARatingView: UIView , UITextFieldDelegate {
    private let titleLabel = UILabel()
    private let contentLabel = UILabel()
    private let paymentDescriptionLabel = UILabel()
    private let cancelButton = UIButton()
    private let confirmButton = UIButton()
    var startInt: Int = 4
    
    lazy var start1Btn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.setImage(UIImage(named: "rate_full"), for: .normal)
        button.addTarget(self, action: #selector(start1Click), for: .touchUpInside)
        return button
    }()
    
    lazy var start2Btn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.setImage(UIImage(named: "rate_full"), for: .normal)
        button.addTarget(self, action: #selector(start2Click), for: .touchUpInside)
        return button
    }()
    
    lazy var start3Btn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.setImage(UIImage(named: "rate_full"), for: .normal)
        button.addTarget(self, action: #selector(start3Click), for: .touchUpInside)
        return button
    }()
    
    lazy var start4Btn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.setImage(UIImage(named: "rate_full"), for: .normal)
        button.addTarget(self, action: #selector(start4Click), for: .touchUpInside)
        return button
    }()
    
    lazy var start5Btn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.setImage(UIImage(named: "rate_empty"), for: .normal)
        button.addTarget(self, action: #selector(start5Click), for: .touchUpInside)
        return button
    }()
    
    var onCancel: (() -> Void)?
    var onConfirm: ((Int?) -> Void)?
    
    init(frame: CGRect, title: String, paymentMethod: String, paymentDescription: String) {
        super.init(frame: frame)
        
        setupUI(title: title, paymentMethod: paymentMethod, paymentDescription: paymentDescription)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(title: String, paymentMethod: String, paymentDescription: String) {
        // 设置自定义弹窗的样式和布局
        self.backgroundColor = .white
        self.layer.cornerRadius = 20
     
        titleLabel.text = "Rating app"
        titleLabel.textColor = .black
        titleLabel.font = UIFont.systemFont(ofSize: 18)
        titleLabel.textAlignment = .center
        titleLabel.numberOfLines = 0
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(29)
            make.height.equalTo(18)
            make.centerX.equalToSuperview()
        }
        
        contentLabel.text = "Do you like this app?\nRating and let us know if you like it"
        contentLabel.textColor = UIColor(hex: "303030")
        contentLabel.font = UIFont.systemFont(ofSize: 15)
        contentLabel.textAlignment = .left
        contentLabel.numberOfLines = 0
        self.addSubview(contentLabel)
        contentLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(20)
            make.left.equalTo(20)
            make.right.equalTo(-20)
        }
        
        self.addSubview(start3Btn)
        start3Btn.snp.makeConstraints { make in
            make.top.equalTo(contentLabel.snp.bottom).offset(22)
            make.centerX.equalToSuperview()
            make.width.equalTo(33)
            make.height.equalTo(31.57)
        }
        
        self.addSubview(start1Btn)
        start1Btn.snp.makeConstraints { make in
            make.top.equalTo(start3Btn)
            make.left.equalTo(23.5)
            make.width.equalTo(33)
            make.height.equalTo(31.57)
        }
        
        self.addSubview(start5Btn)
        start5Btn.snp.makeConstraints { make in
            make.top.equalTo(start3Btn)
            make.right.equalTo(-23.5)
            make.width.equalTo(33)
            make.height.equalTo(31.57)
        }
        
        self.addSubview(start2Btn)
        start2Btn.snp.makeConstraints { make in
            make.top.equalTo(start3Btn)
            make.left.equalTo(start1Btn.snp.right).offset(19.5)
            make.width.equalTo(33)
            make.height.equalTo(31.57)
        }
        
        self.addSubview(start4Btn)
        start4Btn.snp.makeConstraints { make in
            make.top.equalTo(start3Btn)
            make.left.equalTo(start3Btn.snp.right).offset(19.5)
            make.width.equalTo(33)
            make.height.equalTo(31.57)
        }
        
        let screenWidth = UIScreen.main.bounds.width
        cancelButton.setTitle("Later", for: .normal)
        cancelButton.setTitleColor(.black, for: .normal)
        cancelButton.layer.borderWidth = 0.5
        cancelButton.layer.borderColor = UIColor.black.cgColor
        cancelButton.backgroundColor = .clear
        cancelButton.layer.cornerRadius = 20
        self.addSubview(cancelButton)
        cancelButton.snp.makeConstraints { make in
            make.bottom.equalTo(-20)
            make.left.equalTo(20)
            make.width.equalTo(88)
            make.height.equalTo(46)
        }
        
        confirmButton.setTitle("Rate", for: .normal)
        confirmButton.setTitleColor(.white, for: .normal)
        confirmButton.backgroundColor = UIColor(hex: "AB00E4")
        confirmButton.layer.cornerRadius = 20
        self.addSubview(confirmButton)
        confirmButton.snp.makeConstraints { make in
            make.bottom.equalTo(-20)
            make.right.equalTo(-20)
            make.left.equalTo(cancelButton.snp.right).offset(15)
            make.height.equalTo(46)
        }
        
        // 取消按钮点击事件
        cancelButton.addTarget(self, action: #selector(cancelButtonTapped), for: .touchUpInside)
        
        // 确定按钮点击事件
        confirmButton.addTarget(self, action: #selector(confirmButtonTapped), for: .touchUpInside)
    }
    
  
    
    @objc private func cancelButtonTapped() {
        onCancel?()
    }
    
    @objc private func confirmButtonTapped() {
        onConfirm?(self.startInt)
    }
    @objc func start1Click(){
        start1Btn.setImage(UIImage(named: "rate_full"), for: .normal)
        start2Btn.setImage(UIImage(named: "rate_empty"), for: .normal)
        start3Btn.setImage(UIImage(named: "rate_empty"), for: .normal)
        start4Btn.setImage(UIImage(named: "rate_empty"), for: .normal)
        start5Btn.setImage(UIImage(named: "rate_empty"), for: .normal)
        startInt = 1
    }
    @objc func start2Click(){
        start1Btn.setImage(UIImage(named: "rate_full"), for: .normal)
        start2Btn.setImage(UIImage(named: "rate_full"), for: .normal)
        start3Btn.setImage(UIImage(named: "rate_empty"), for: .normal)
        start4Btn.setImage(UIImage(named: "rate_empty"), for: .normal)
        start5Btn.setImage(UIImage(named: "rate_empty"), for: .normal)
        startInt = 2
    }
    @objc func start3Click(){
        start1Btn.setImage(UIImage(named: "rate_full"), for: .normal)
        start2Btn.setImage(UIImage(named: "rate_full"), for: .normal)
        start3Btn.setImage(UIImage(named: "rate_full"), for: .normal)
        start4Btn.setImage(UIImage(named: "rate_empty"), for: .normal)
        start5Btn.setImage(UIImage(named: "rate_empty"), for: .normal)
        startInt = 3
    }
    @objc func start4Click(){
        start1Btn.setImage(UIImage(named: "rate_full"), for: .normal)
        start2Btn.setImage(UIImage(named: "rate_full"), for: .normal)
        start3Btn.setImage(UIImage(named: "rate_full"), for: .normal)
        start4Btn.setImage(UIImage(named: "rate_full"), for: .normal)
        start5Btn.setImage(UIImage(named: "rate_empty"), for: .normal)
        startInt = 4
    }
    @objc func start5Click(){
        start1Btn.setImage(UIImage(named: "rate_full"), for: .normal)
        start2Btn.setImage(UIImage(named: "rate_full"), for: .normal)
        start3Btn.setImage(UIImage(named: "rate_full"), for: .normal)
        start4Btn.setImage(UIImage(named: "rate_full"), for: .normal)
        start5Btn.setImage(UIImage(named: "rate_full"), for: .normal)
        startInt = 5
    }
}
